/*
	Time.h
	Christopher Raffaele
	04.28.2008

	Time's class definitions
*/
#include"TimeConverter.h" //Time class declarations

//creates the conversion table and unit string array
TimeConverter::TimeConverter(){
	//allocate memory for the conversion table
	conversionTable = new double*[4];

	for(int i = 0; i < 4; ++i)
		conversionTable[i] = new double[4];

	createConversionTable();

	//allocate memory for the units string
	units = new std::string[4];

	createUnitsString();
}

//deallocates dynamic memory used in the table and unit string array
TimeConverter::~TimeConverter(){
	//delete the conversion table
	for(int i = 0; i < 4; ++i)
		delete [] conversionTable[i];

	delete [] conversionTable;
	delete [] units;
}

//create the table used to calculate conversions
void TimeConverter::createConversionTable(){
	setSecondsRow();
	setMinutesRow();
	setHoursRow();
	setYearsRow();
}

//set the values for conversions involving seconds
void TimeConverter::setSecondsRow(){
	conversionTable[0][0] = 1.0;
	conversionTable[0][1] = 0.01666666666666667;
	conversionTable[0][2] = 0.00027777777777778;
	conversionTable[0][3] = 0.00000003170979198;
}

//display the first menu
void TimeConverter::showMainMenu(){
	std::cout << "\n--------------------------------\n"
		 << "\\       Time                    \\\n"
		 << " ---------------------------------\n"
		 << " | 1 | Seconds                   |\n"
		 << " ---------------------------------\n"
		 << " | 2 | Minutes                   |\n"
		 << " ---------------------------------\n"
		 << " | 3 | Hours                     |\n"
		 << " ---------------------------------\n"
 		 << " | 4 | Years                     |\n"
		 << " ---------------------------------\n";
}

double TimeConverter::getTimeToConvert(){
	cout << "\nEnter the amount of time you would like to convert: ";

	double time = 0;

	if(cin.width() != 0) cin.ignore(1000, '\n');

	cin >> time;

	//invalid input
	if(cin.fail()){
		cin.clear();
		cin.ignore(1000, '\n');
		time = 0;
	}

	return time;
}

unsigned int TimeConverter::getConvertFromToChoice(bool from){
	//determine which message to show
	if(from)
		cout << "\nWhat units would you like to convert from? ";
	else
		cout << "\nWhat units would you like to convert to? ";

	unsigned int userChoice = 0;

	if(cin.width() != 0) cin.ignore(1000, '\n');

	//input validation
	do{
		cin >> userChoice;

		//invalid input
		if(cin.fail()){
			cin.clear();
			cin.ignore(1000, '\n');
			userChoice = 0;
		}
	//input must be 1 - 4
	}while(userChoice < 1 || userChoice > 4);

	return userChoice;
}

//performs a time conversion
void TimeConverter::run(){
	showMainMenu();

	unsigned int convertFrom = getConvertFromToChoice(true);
	unsigned int convertTo = getConvertFromToChoice(false);

	double timeToConvert = getTimeToConvert();

	//print answer
	cout << "\n" << timeToConvert << " " << units[convertFrom -1].c_str() << " is equal to "
		 << timeToConvert * conversionTable[convertFrom - 1][convertTo - 1]
		 << " " << units[convertTo - 1].c_str();
}

//checks if the user wants to continue converting time
bool TimeConverter::userWantsToRunAgain(){
	cout << "\nCalculate more time conversions(y/n)? ";

	cin.ignore(1000, '\n');

	//check only the first char
	return (cin.get() == 'y');
}

//ensures the user can keep converting time until the user wants to quit
void TimeConverter::start(){
	do{
		run();
	}while(userWantsToRunAgain());
}

//sets the values to use for outputing the proper units the user selected
void TimeConverter::createUnitsString(){
	units[0] = "seconds(s)";
	units[1] = "minutes(min)";
	units[2] = "hours(hr)";
	units[3] = "years(y)";
}

//set the values for conversions involving minutes
void TimeConverter::setMinutesRow(){
	conversionTable[1][0] = 60.0;
	conversionTable[1][1] = 1.0;
	conversionTable[1][2] = 0.01666666666666667;
	conversionTable[1][3] = 0.00000190258751903;
}

//set the values for conversions involving hours
void TimeConverter::setHoursRow(){
	conversionTable[2][0] = 3600.0;
	conversionTable[2][1] = 60.0;
	conversionTable[2][2] = 1.0;
	conversionTable[2][3] = 0.00011415525114155;
}

//set the values for conversions involving years
void TimeConverter::setYearsRow(){
	conversionTable[3][0] = 31536000.0;
	conversionTable[3][1] = 525600.0;
	conversionTable[3][2] = 8760.0;
	conversionTable[3][3] = 1.0;
}

